package com.leancloud.login.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.login.LoginPath;
import com.leancloud.login.R;

import tech.com.commoncore.LoginInterceptorImpl;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.ToastUtil;

@Route(path = LoginPath.LOGIN_FEEDBACK,extras = LoginInterceptorImpl.LOGINCHECK)
public class FeedBackActivity extends BaseTitleActivity implements View.OnClickListener {

    private EditText etFeedback;
    private TextView textCountTx;

    private TextView typeTx1, typeTx2, typeTx3;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText(getResources().getString(R.string.login_feedback));
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        etFeedback = findViewById(R.id.et_feedback);
        textCountTx = findViewById(R.id.text_count_tx);
        typeTx1 = findViewById(R.id.feedback_tx_1);
        typeTx2 = findViewById(R.id.feedback_tx_2);
        typeTx3 = findViewById(R.id.feedback_tx_3);

        findViewById(R.id.btn_submit).setOnClickListener(this);
        typeTx1.setOnClickListener(this);
        typeTx2.setOnClickListener(this);
        typeTx3.setOnClickListener(this);

        selectFeedBack(R.id.feedback_tx_1);

        setTextCountTx();

        etFeedback.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setTextCountTx();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_submit) {
            onViewClicked();
        }
        if (v.getId() == R.id.feedback_tx_1 || v.getId() == R.id.feedback_tx_2 || v.getId() == R.id.feedback_tx_3) {
            selectFeedBack(v.getId());
        }
    }

    private void selectFeedBack(int id) {
        if (id == R.id.feedback_tx_1) {
            setTextViewStyle(typeTx1, true);
            setTextViewStyle(typeTx2, false);
            setTextViewStyle(typeTx3, false);
        }
        if (id == R.id.feedback_tx_2) {
            setTextViewStyle(typeTx1, false);
            setTextViewStyle(typeTx2, true);
            setTextViewStyle(typeTx3, false);
        }
        if (id == R.id.feedback_tx_3) {
            setTextViewStyle(typeTx1, false);
            setTextViewStyle(typeTx2, false);
            setTextViewStyle(typeTx3, true);
        }
    }

    private void setTextViewStyle(TextView tx, boolean select) {
        if (select) {
//            tx.setBackground(getResources().getDrawable(R.drawable.my_bg_feedback_true));
//            tx.setTextColor(getResources().getColor(R.color.my_color_feedback_true));
        } else {
//            tx.setBackground(getResources().getDrawable(R.drawable.my_bg_feedback_false));
//            tx.setTextColor(getResources().getColor(R.color.my_color_feedback_false));
        }
    }

    private void setTextCountTx() {
        String idea = etFeedback.getText().toString();
        String count = String.valueOf(idea.length());
        textCountTx.setText(count + "/255");
    }

    public void onViewClicked() {
        String content = etFeedback.getText().toString().trim();
        if (DataUtils.isEmptyString(content)) {
            ToastUtil.show(getResources().getString(R.string.login_feedback_content_hint));
            return;
        }
        showLoading();
        mContentView.postDelayed(new Runnable() {
            @Override
            public void run() {
                hideLoading();
                ToastUtil.show(getResources().getString(R.string.login_feedback_hint));
                finish();
            }
        }, 1000);

    }
}
